<?php

namespace common\behaviors;

use yii\behaviors\AttributeBehavior;
use yii\db\BaseActiveRecord;

class TimestampBehavior extends AttributeBehavior
{
    /**
     * @var string the attribute that will receive timestamp value
     * Set this property to false if you do not want to record the creation time.
     */
    public $createdAtAttribute = 'created_at';

    /**
     * @var string the attribute that will receive timestamp value.
     * Set this property to false if you do not want to record the update time.
     */
    public $updatedAtAttribute = 'updated_at';

    /**
     * @var string 日期格式
     */
    public $dateFormat = 'Y-m-d H:i:s';

    /**
     * @var bool 是否自动更新
     */
    public $autoUpdateTime = true;

    /**
     * {@inheritdoc}
     */
    public $value;


    public function timestampBehaviorRules()
    {
        return [[$this->createdAtAttribute, $this->updatedAtAttribute], 'string'];
    }

    /**
     * @inheritdoc
     */
    public function init()
    {
        parent::init();
        if (empty($this->attributes))
        {
            $this->attributes = [
                BaseActiveRecord::EVENT_BEFORE_INSERT => [$this->createdAtAttribute, $this->updatedAtAttribute],
                BaseActiveRecord::EVENT_BEFORE_UPDATE => $this->updatedAtAttribute,
                BaseActiveRecord::EVENT_AFTER_FIND    => [$this->createdAtAttribute, $this->updatedAtAttribute],
            ];
        }
    }

    /**
     * 直接设置时间值
     *
     * @param \yii\base\Event $event
     */
    public function evaluateAttributes($event)
    {
        if ( ! empty($this->attributes[$event->name]))
        {
            if ($event->name === BaseActiveRecord::EVENT_AFTER_FIND)
            {
                $this->owner->{$this->createdAtAttribute} = date($this->dateFormat, $this->owner->{$this->createdAtAttribute});
                $this->owner->{$this->updatedAtAttribute} = date($this->dateFormat, $this->owner->{$this->updatedAtAttribute});
            }

            if ($event->name === BaseActiveRecord::EVENT_BEFORE_UPDATE || $event->name === BaseActiveRecord::EVENT_BEFORE_INSERT)
            {
                if ($this->owner->{$this->createdAtAttribute} == null)
                {
                    $this->owner->{$this->createdAtAttribute} = date($this->dateFormat);
                }

                if (
                    $this->owner->{$this->updatedAtAttribute} == null
                    ||
                    ($event->name === BaseActiveRecord::EVENT_BEFORE_UPDATE && $this->autoUpdateTime)
                )
                {
                    $this->owner->{$this->updatedAtAttribute} = date($this->dateFormat);
                }

                if ( ! is_numeric($this->owner->{$this->createdAtAttribute}))
                {
                    $this->owner->{$this->createdAtAttribute} = strtotime($this->owner->{$this->createdAtAttribute});
                }
                if ( ! is_numeric($this->owner->{$this->updatedAtAttribute}))
                {
                    $this->owner->{$this->updatedAtAttribute} = strtotime($this->owner->{$this->updatedAtAttribute});
                }
            }
        }
    }
}
